//
//  ShellChangeTeadePasswordViewController.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellChangeTeadePasswordViewController.h"
#import "ShellAccountTextField.h"
#import "ShellAccountViewModel.h"
#import "YN_PassWordView.h"

@interface ShellChangeTeadePasswordViewController ()
@property (nonatomic,strong) UIButton *verificationBtn;

//取消订阅RAC信号
@property (nonatomic,strong) RACDisposable *disposable;
@property (nonatomic,assign) NSInteger timeCount;
@property (nonatomic,strong) ShellAccountViewModel *viewModel;

@end

@implementation ShellChangeTeadePasswordViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = LZLocalizedString(@"修改交易密码");
    
    [self setupView];
    
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //查看堆栈, 看是否有该VC
    NSArray * navs = self.navigationController.viewControllers;
    BOOL isDispose = YES;
    for (UIViewController * vc in navs){
        NSString * VCString =  NSStringFromClass([vc class]);
        NSString * orderVCString =  NSStringFromClass([self class]);
        if([VCString isEqualToString:orderVCString]){
            isDispose = NO;
        }
    }
    if(isDispose) {
        //释放定时器
        [self.disposable dispose];
        self.disposable = nil;
    }
}




- (void)setupView{
    
 
    ///!!!:原密码
    ShellAccountTextField *codeTextField = [[ShellAccountTextField alloc] init];
    [self.view addSubview:codeTextField];
    codeTextField.placeholder = LZLocalizedString(@"邮箱验证码");
    [codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    self.verificationBtn.frame = CGRectMake(0, 0, 130, 50);
    codeTextField.rightView = self.verificationBtn;
    codeTextField.rightViewMode = UITextFieldViewModeAlways;

    
    ///!!!:交易密码
    YN_PassWordView *codeView = [[YN_PassWordView alloc] init];
    CGFloat itemWidth = (K_SCREENWIDTH - 30 - 8 * 5)/ 6;
    codeView.frame = CGRectMake(15, 110, K_SCREENWIDTH - 30 ,itemWidth);

    [self.view addSubview:codeView];
    codeView.showType = 3;//五种样式
    codeView.num = 6;//框框个数
    codeView.tintColor = UIColorHex(0x11385E);//主题色
    [codeView show];
    
    ///!!!:提交
    UIButton *submitBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"提交") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    [submitBtn setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [self.view addSubview:submitBtn];
    submitBtn.cornerRadius = 6;
    submitBtn.layer.shadowOffset = CGSizeMake(0,2);
    submitBtn.layer.shadowOpacity = 1;
    submitBtn.layer.shadowRadius = 6;
    submitBtn.enabled = YES;
    [submitBtn setTitleColor:RGBA(255, 255, 255, 0.5) forState:UIControlStateDisabled];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(codeView.mas_bottom).mas_offset(26);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
 
    @weakify(self);
    [[submitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        ///!!!:去提交修改密码
        [self.viewModel changeTradeVeriCode:codeTextField.text password:codeView.textF.text];
    }];
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self startDisposable];
    }];
}
//发送验证码
- (void)verificationCodeClick:(UIButton *)btn{
    ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
    [self.viewModel sendVerifiCode:detailModel.email type:3];
}

//开启倒计时
- (void)startDisposable {
    
    @weakify(self)
    [self.view endEditing:YES];
    self.timeCount = K_timeCount;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.verificationBtn setTitle:[NSString stringWithFormat:@"%ldS",(long)self.timeCount] forState:self.timeCount > 0 ? UIControlStateDisabled:UIControlStateNormal];
    });
    
    self.verificationBtn.enabled = NO;
    
    //button type要 设置成custom 否则会闪动
    self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * _Nullable x) {
        @strongify(self)
        //时间 --
        NSLog(@"发送验证码 %ld",(long)self.timeCount);
        
        self.timeCount -- ;
        if(self.timeCount < 1){
            self.verificationBtn.enabled = YES;
            self.timeCount  = 0;
            [self.disposable dispose];
            self.disposable = nil;
        } else {
            self.verificationBtn.enabled = NO;
        }
        //设置文字
        NSString * btnText = self.timeCount > 0 ? [NSString stringWithFormat:@"%ldS",(long)self.timeCount]:[NSString stringWithFormat:@"%@",LZLocalizedString(@"重新发送")];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.verificationBtn.frame = CGRectMake(0, 0, 130, 50);
            [self.verificationBtn setTitle:btnText forState:self.timeCount > 0 ? UIControlStateDisabled:UIControlStateNormal];
        });
    }];
    
}

- (UIButton *)verificationBtn{
    if(!_verificationBtn){
        _verificationBtn = [[UIButton alloc] init];
        [_verificationBtn setTitle:[NSString stringWithFormat:@"%@",LZLocalizedString(@"获取验证码")] forState:UIControlStateNormal];
        [_verificationBtn setTitleColor:UIColorHex(0x4AB2E7) forState:UIControlStateNormal];
        _verificationBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_verificationBtn addTarget:self action:@selector(verificationCodeClick:) forControlEvents:UIControlEventTouchUpInside];
        _verificationBtn.enabled = YES;
    }
    return _verificationBtn;
}

//viewModel
- (ShellAccountViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ShellAccountViewModel alloc] init];
    }
    return _viewModel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
